import base64
import datetime
import json

from flask import Flask, request, jsonify, render_template_string
from langfuse import Langfuse
import requests
import os
import uuid

app = Flask(__name__)

@app.route('/manual-score/<trace_id>')
def manual_score(trace_id):
    """手动创建分数上传请求"""
    try:
        # 准备请求数据
        data = {
            "batch": [{
                "id": str(uuid.uuid4()),
                "type": "score-create",
                "timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
                "body": {
                    "id": str(uuid.uuid4()),
                    "traceId": trace_id,
                    "name": "manual_test_score",
                    "value": 0.9,
                    "comment": "手动测试分数"
                }
            }],
            "metadata": {
                "batch_size": 1,
                "sdk_name": "python",
                "sdk_version": "3.3.4",
                "public_key": "pk-1f-41d47442-ef86e1b-806d-fd8ab170970a"
            }
        }
        
        # 发送请求 - 使用Bearer认证
        response = requests.post(
            "https://langfuse-dev.chinabeego.com/api/public/ingestion",
            headers={
                "Authorization": f"Bearer sk-1f-0a654f31-cd55-4dfe-8896-cd38404ef23e",
                "Content-Type": "application/json"
            },
            json=data,
            timeout=10
        )
        
        if response.status_code == 200:
            return jsonify({
                "status": "success",
                "message": "手动分数上传成功",
                "response": response.json()
            })
        else:
            return jsonify({
                "status": "error",
                "message": f"手动分数上传失败: {response.status_code}",
                "response_text": response.text
            }), response.status_code
            
    except Exception as e:
        return jsonify({
            "status": "error",
            "message": f"手动分数上传异常: {str(e)}"
        }), 500


if __name__ == '__main__':
    app.run(debug=True)